﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using TciNewsReader_2014_04.VmHelpers;
namespace TciNewsReader_2014_04
{
    public partial class ViewModel
    {
        public ObservableCollection<SortInfo> SortSelectors { get; set; }
        private SortInfo _sortSelectedItem;
        public SortInfo SortSelectedItem
        {// binded to the menu
            get { return _sortSelectedItem; }
            set
            {
                if (value != _sortSelectedItem)
                {
                    _sortSelectedItem = value;
                    OnPropertyChanged("SortSelectedItem");
                    UpdateSortStatus();
                    SortTheItems();
                }
            }
        }
        private string _sortStatus = String.Empty;
        public string SortStatus
        {// displays in the status bar
            [DebuggerStepThrough] get { return _sortStatus; }
            [DebuggerStepThrough]
            set
            {
                if (value != _sortStatus)
                {
                    _sortStatus = value;
                    OnPropertyChanged("SortStatus");
                }
            }
        }
        private string _sortStatusShortName;
        public string SortStatusShortName
        {// displays in the status bar
            [DebuggerStepThrough] get { return _sortStatusShortName; }
            [DebuggerStepThrough]
            set
            {
                if (value != _sortStatusShortName)
                {
                    _sortStatusShortName = value;
                    OnPropertyChanged("SortStatusShortName");
                }
            }
        }
        private void SortTheItems()
        {
            if (ItemWrappers != null && ItemWrappers.Count > 1)
            {
                _itemWrappersListCollectionView.SortDescriptions.Clear();
                _itemWrappersListCollectionView.SortDescriptions.Add(new SortDescription(SortSelectedItem.PropertyName, SortSelectedItem.Direction));
            }
        }
        private void UpdateSortStatus()
        {
            SortStatus = String.Format("{0}", SortSelectedItem.Description);
            SortStatusShortName = String.Format("{0}", SortSelectedItem.ShortName);
        }
        private void InitialiseSorts()
        {
            SortSelectors = new ObservableCollection<SortInfo>
            {
                new SortInfo {Description = "Publication date (newest first)", ShortName = "Date (v)", Direction = ListSortDirection.Descending, PropertyName = "ItemPublishDate"},
                new SortInfo {Description = "Publication date (oldest first)", ShortName = "Date (^)", Direction = ListSortDirection.Ascending, PropertyName = "ItemPublishDate"},
                new SortInfo {Description = "FeedPublisherName (alpha)", ShortName = "Source (^)", Direction = ListSortDirection.Ascending, PropertyName = "FeedPublisherName"},
                new SortInfo {Description = "FeedPublisherName (reverse alpha)", ShortName = "Source (v)", Direction = ListSortDirection.Descending, PropertyName = "FeedPublisherName"},
            };
            SortSelectors[0].IsChecked = true;
            SortSelectedItem = SortSelectors[0];
            _itemWrappersListCollectionView.SortDescriptions.Clear();
            _itemWrappersListCollectionView.SortDescriptions.Add(new SortDescription(SortSelectedItem.PropertyName, SortSelectedItem.Direction));
            foreach (SortInfo si in SortSelectors)
            {
                si.PropertyChanged += (s, a) =>
                {
                    if (a.PropertyName != "IsChecked") return;
                    var sortInfo = s as SortInfo;
                    if (sortInfo != null)
                    {
                        if (!sortInfo.IsChecked) return;
                        foreach (SortInfo sinfo in SortSelectors)
                        {
                            if (sinfo.Description != sortInfo.Description)
                            {
                                sinfo.IsChecked = false;
                            }
                        }
                        SortSelectedItem = sortInfo;
                    }
                };
            }
        }
    }
}